{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.8.1-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python38164bitc9425b4fd2d44566b8cda82b00a1f4f8",
   "display_name": "Python 3.8.1 64-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "import scipy.spatial.distance as ssd \n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>item1</th>\n      <th>item2</th>\n      <th>item3</th>\n      <th>item4</th>\n      <th>item5</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>user1</th>\n      <td>1</td>\n      <td>4</td>\n      <td>2</td>\n      <td>1</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>user2</th>\n      <td>2</td>\n      <td>4</td>\n      <td>2</td>\n      <td>1</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>user3</th>\n      <td>5</td>\n      <td>1</td>\n      <td>5</td>\n      <td>4</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>user4</th>\n      <td>2</td>\n      <td>5</td>\n      <td>3</td>\n      <td>4</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>",
      "text/plain": "       item1  item2  item3  item4  item5\nuser1      1      4      2      1      0\nuser2      2      4      2      1      5\nuser3      5      1      5      4      2\nuser4      2      5      3      4      5"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_data = pd.read_csv('./test_data1.csv', sep=',')\n",
    "df_data.index = ['user1','user2','user3','user4']\n",
    "df_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "user2    2.8\nuser3    3.4\nuser4    3.8\ndtype: float64"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_mean = df_data.mean(axis=1)[1:]\n",
    "user_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([-0.84561791,  0.88176419, -0.88176419, -0.83262711])"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def sim(v1, v2):\n",
    "    return 1- ssd.cosine(v1 - user_mean, v2- user_mean)\n",
    "sim_v = np.array([\n",
    "    sim (df_data['item5'][1:], df_data[item][1:])\n",
    "    for item in ['item1','item2','item3','item4',]\n",
    "])\n",
    "sim_v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "0.024778902847947197"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# predict\n",
    "predict_rating = np.dot(sim_v, df_data.iloc[0][0:4])/sum(np.abs(sim_v))\n",
    "np.clip(predict_rating, 0,5)"
   ]
  }
 ]
}